﻿using BLL;
using Model;
using System;

namespace BaseUI
{
    public class BaseControl : System.Web.UI.UserControl
    {
        private Sys_UserBLL user = new Sys_UserBLL();
        public Sys_User lognUser;

        public BaseControl()
        {
            this.Load += new EventHandler(BaseControl_Load);
        }

        private void BaseControl_Load(object sender, EventArgs e)
        {
            if (!IsAdminLogin())
            {
                ReLogin();
                //Response.Write("<script>alert('请先登录!!!');window.parent.location.href='Login.aspx'</script>");
                //Response.End();
            }
        }

        public void ReLogin()
        {
            string str = Request.Url.AbsoluteUri;
            if (str.IndexOf(":80") > 0)
            {
                str = Request.Url.AbsoluteUri.Substring(0, Request.Url.AbsoluteUri.IndexOf(":80") + 6) + "Login.aspx";
            }
            else
            {
                str = Request.Url.AbsoluteUri.Substring(0, Request.Url.AbsoluteUri.IndexOf("/") + 1) + "Login.aspx";
            }
            string scriptString = "<script>window.opener=null ;window.parent.location='" + str + "';</script>";
            Response.Write(scriptString);
            Response.End();
        }

        /// <summary>
        /// 判用户是否已经登录(解决Session超时问题)
        /// </summary>
        public bool IsAdminLogin()
        {
            if (System.Web.HttpContext.Current.Session["SysUserInfo"] != null)
            {
                try
                {
                    lognUser = (Sys_User)System.Web.HttpContext.Current.Session["SysUserInfo"];
                }
                catch (Exception)
                {
                    throw;
                }
                return true;
            }
            else
            {
                return false;
            }
        }
    }
}